<?php defined('BASEPATH') or exit('No direct script access allowed');

class Member_User_model extends CI_Model
{
	private $sid ;
	private $user_table ;
	private $integral_table ;
	private $user_expand_table ;
	public function __construct()
	{
		parent::__construct();
		$this->sid = $GLOBALS['sid'];
		$this->data = $GLOBALS['config'];
		$this->user_table = $this->db->dbprefix('user');
		$this->integral_table = $this->db->dbprefix('user_integral_rule');
		$this->user_expand_table = $this->db->dbprefix('user_expand');
		$this->user_favourite_table = $this->db->dbprefix('user_favourite');
		$this->line_table = $table = $this->db->dbprefix("line");
        $this->car_info_table = $this->db->dbprefix('car_info');
        $this->scenic_table      = $this->db->dbprefix("scenic");
	}



	/**
	 * 获取单条记录
	 * @param array $where 查询条件
	 * @param string $field 显示字段
	 * @return mixed
	 */
	function get_row($where=array(),$field='*'){
		if(!empty($field))$this->db->select($field);
		//if(!isset($where['sid']))$where['sid'] = $this->sid;
		$query = $this->db->get_where($this->user_table,$where,1);
		return $query->row_array();
	}

	function add_row($data)
	{
		$table = $this->user_table;
		if (is_array($data)) {
			if ($this->db->insert($table, $data)) {
				return $this->db->insert_id();
			} else {
				return false;
			}
		}
	}
	function add_row_expand($data)
	{
		$table = $this->user_expand_table;
		if (is_array($data)) {
			if ($this->db->insert($table, $data)) {
				return $this->db->insert_id();
			} else {
				return false;
			}
		}
	}

	function edit_row($data,$where){
		if(!empty($where))$this->db->where($where, NULL, FALSE);
		return $this->db->update($this->user_table,$data);
	}

	function edit_expand_row($data,$where){
		if(!empty($where))$this->db->where($where, NULL, FALSE);
		return $this->db->update($this->user_expand_table,$data);
	}


	/**
	 * 用户/微店 信息
	 * @param $user_id
	 * @return array
	 */
	public function _user_info($user_id)
	{
		$t = $this->user_table;
		$ex_t = $this->user_expand_table;
		$where = array("$t.user_id"=>$user_id);
		$this->db->select('*');
		$this->db->from($t);
		$this->db->join($ex_t,"$t.user_id = $ex_t.user_id");
		$this->db->where($where);
		$query = $this->db->get();
		$res = $query->row_array();
		return $res ;
	}


 

	/**
	 * 积分规则信息
	 * @param $rid
	 * @return array
	 */
	public function _integral_rule_info($rid)
	{
		$t = $this->integral_table;
		$query = $this->db->get_where($t, array('rid' => $rid),'1');
		$res = $query->row_array();
		return $res ;
	}


	/**
	 *
	 * 删除会员/微店
	 * @param array $where 删除条件
	 */

	public function _delete_member($where=array()){
		$t = $this->user_table;
		if(!empty($where)&&is_array($where)){
			return $this->db->delete($t,$where);
		}elseif(is_string($where)){
			$this->db->where($where);
			return $this->db->delete($t);
		}
		return false;
	}

	/**
	 *
	 * 删除积分规则
	 * @param array $where 删除条件
	 */
	public function _delete_rule($where=array()){
		$t = $this->integral_table;
		if(!empty($where)&&is_array($where)){
			return $this->db->delete($t,$where);
		}elseif(is_string($where)){
			$this->db->where($where);
			return $this->db->delete($t);
		}
		return false;
	}

	/**
	 * 添加会员
	 * @param $info_array
	 * @return true/false
	 */
	public function _add_member($info)
	{
		$t= $this->user_table;
		$ex_t = $this->user_expand_table;
		$user_name = $info['username'];
		$mobile = $info['mobile'] ? $info['mobile'] : '';
		$tl_status = $info['tl_status'] ? $info['tl_status'] : 0;
		$company = $info['company'] ? $info['company'] : '';
		$realname = $info['realname'] ? $info['realname'] : '';
		$check_status = 0;
		if($check_status=='0')
		{
			$mix = $this->_rand_6_char();
			$password = md5(md5($info['password'].$mix));
			$login_date = $reg_date = date('Y-m-d H:i:s');
			$login_ip = $reg_ip = $this->input->ip_address();
			$status = $info['status'] ;
			$array = array(
             'sid'=>$info['sid'] ,
			 'mobile'=>$mobile ,
             'username'=>$user_name ,
             'password'=>$password ,
             'login_date'=>$login_date ,
             'reg_date'=>$reg_date ,
             'login_ip'=>$login_ip ,
             'reg_ip'=>$reg_ip ,
             'status'=>$status ,
			 'tl_status'=>$tl_status ,
             'mix'=>$mix ,
			);
			$this->db->insert($t,$array);
			$user_id = $this->db->insert_id();
			$this->db->insert($ex_t,array('user_id'=>$user_id,'company'=>$company,'realname'=>$realname));
			if($user_id) return $user_id ;
		}
	}


	public function add_expand_row($array = array())
	{

		$this->db->insert($this->user_expand_table,$array);
		return $this->db->insert_id();
	}



	/**
	 * 添加积分规则
	 * @param $info_array
	 * @return true/false
	 */
	public function _add_rule($array)
	{
		$t= $this->integral_table;
		$check_status = $this->_integral_action_ver(array('sid'=>$array['sid'],'action'=>$array['action']));
		if($check_status[1]=='200'){
			$this->db->insert($t,$array);
			$rid = $this->db->insert_id();
			return  array('action_pass','200',true);
		}
		return $check_status ;
	}

	/**
	 * 修改积分规则
	 * @param $array
	 * @return true/false
	 */
	public function _edit_rule($array,$where)
	{
		$t= $this->integral_table;
		if(!empty($where))$this->db->where($where);
		return $this->db->update($t,$array);
		return false;
	}

	/**
	 * 登陆验证模型
	 * @param account
	 * @param password
	 * @return array
	 */
	public function _login_verify($account,$password,$auto='')
	{
		$table = $this->user_table;
		$where = array('username'=>$account,'status'=>'1');
		if($this->_check_is_email($account))   $where = array('email'=>$account);
		if($this->_check_is_mobile($account))  $where = array('mobile'=>$account);
		$where['sid'] = $this->sid ;
		$query = $this->db->get_where($table,$where,'1');
		$ver_data = $query->row_array();
		if(empty($ver_data))
		{
			return array('status'=>'0','info' => '账号不存在','msg' => 'not_exist_account');
		}
		if($ver_data['status']!=='1')
		{
			return array('status'=>'0','info' => '账号已冻结','msg' => 'invalid_account');
		}
		$tem = md5(md5($password.$ver_data['mix']));
		if($tem==$ver_data['password'])
		{
			if($auto=='on') $this->session->sess_expiration = '20160';
			if(!empty($ver_data['username'])) $identity_sign = $ver_data['username'] ;
			if(!empty($ver_data['email']))    $identity_sign = $ver_data['email'] ;
			if(!empty($ver_data['mobile']))   $identity_sign = $ver_data['mobile'] ;
			$this->session->set_userdata('user_id',$ver_data['user_id']);
			$this->session->set_userdata('username',$ver_data['username']);
			$this->session->set_userdata('email',$ver_data['email']);
			$this->session->set_userdata('mobile',$ver_data['mobile']);
			$this->session->set_userdata('tl_status',$ver_data['tl_status']);
			$this->session->set_userdata('identity_sign',$identity_sign);
			$this->session->set_userdata('is_login','1');
			$this->_integral_operate('login',$ver_data['user_id']) ;
			return array('status'=>'1','info' => '登陆成功','msg' => 'login_success','data'=>$ver_data['user_id']);
		}
		return array('status'=>'0','info' => '帐号或密码不正确','msg' => 'login_fail');
	}



	/**
	 * 注册验证模型
	 * @param $account      email/username/telphone
	 * @param $password
	 * @param $username  可为空
	 * @return array
	 */

	public function _register_verify($username='',$account,$password,$tl_status=0)
	{
		$t = $this->user_table;
		$sid = $this->sid;
		if($this->_check_is_mobile($account))
		{
			$status = $this->_mobile_ver($account);
			if($status['status']=='1')
			{
				$mobile = $account ;
			}
			else
			{
				return $status ;
			}
		}
		if($this->_check_is_email($account))
		{
			$status = $this->_email_ver($account);
			if($status['status']=='1')
			{
				$email = $account ;
			}
			else
			{
				return $status ;
			}
		}

		if(!empty($username)&&$this->_check_is_username($username))
		{
			$status = $this->_username_ver($username);
			if($status['status']!=='1')
			{
				return $status ;
			}
		}
		$reg_date  = date('Y-m-d H:i:s');
		$login_date = date('Y-m-d H:i:s');
		$reg_ip = $this->input->ip_address();
		$login_ip = $this->input->ip_address();
		$mix = $this->_rand_6_char() ;
		$password = md5(md5($password.$mix));
		$data = array(
            'sid' => $sid ,
            'password' => $password,
            'reg_date' => $reg_date,
            'login_date' => $login_date,
            'reg_ip' => $reg_ip ,
            'login_ip' => $login_ip,
            'status' => '1',
			'tl_status' => $tl_status,
            'mix'=>$mix

		);
		if(!empty($username)) $data['username']= $username ;
		if(!empty($email))    $data['email']= $email ;
		if(!empty($mobile))   $data['mobile']= $mobile ;
		$query = $this->db->insert($t,$data);
		$user_id = $this->db->insert_id();
		if($user_id)
		{
			$this->_integral_operate('register',$user_id) ;
			return array('status'=>'1','info' => '注册成功','msg' => 'register_success','data'=>$user_id);
		}
		return array('status'=>'0','info' => '注册失败','msg' => 'register_fail');

	}

	/**
	 * 找回密码模型
	 * @param $account      email/mobile
	 * @param $password
	 * @return array
	 */

	public function _unset_password($account,$password)
	{
		if($this->_check_is_email($account))   $where = array('email'=>$account);
		if($this->_check_is_mobile($account))  $where = array('mobile'=>$account);
		$mix = $this->_rand_6_char() ;
		$password = md5(md5($password.$mix));
		$data = array('password'=>$password,'mix'=>$mix);
		$t = $this->db->dbprefix('user');
		$this->db->where($where);
		$res = $this->db->update($t,$data);
		if($res)
		{
			return array('status'=>'1','info' => '找回密码成功');
		}
		return array('status'=>'0','info' => '找回密码失败');

	}

	/**
	 * 修改密码模型
	 * @param $old
	 * @param $password
	 * @return array
	 */

	public function _modify_password($old,$password)
	{
		$t = $this->user_table ;
		$user_id  = $this->session->userdata('user_id');
		$where = array('user_id'=>$user_id);
		$query = $this->db->get_where($t,$where,'1');
		$ver_data = $query->row_array();
		$tem = md5(md5($old.$ver_data['mix']));
		$verify = FALSE ;
		if($tem==$ver_data['password'])
		{
			$mix = $this->_rand_6_char() ;
			$password = md5(md5($password.$mix));
			$data = array('password'=>$password,'mix'=>$mix);
			$this->db->where($where);
			$verify = $this->db->update($t,$data);
		}
		if($verify)
		{
			$this->session->unset_userdata('username');
			$this->session->unset_userdata('user_id');
			$this->session->unset_userdata('is_login');
			return array('status'=>'1','info' => '修改密码成功');
		}
		return array('status'=>'0','info' => '修改密码失败');
	}

	/**
	 * 验证邮箱模型
	 * @param $email      email
	 * @return array
	 */
	public function _email_ver($email,$user_id='')
	{
		$t = $this->user_table;
		$where = array('email'=>$email,'sid'=>$this->sid) ;//,'sid'=>$this->sid
		if(empty($user_id))
		{
			$query = $this->db->get_where($t,$where,1);
		}
		else
		{
			$where['user_id !='] = $user_id ;
			$query = $this->db->get_where($t,$where,1);
		}
		$row = $query->row_array();
		if($row)
		{
			return  array('status'=>'0','info'=>'邮箱重复','msg'=>'email_repeat');
		}
		return  array('status'=>'1','info'=>TRUE,'msg'=>'email_pass');
	}


	/**
	 * 验证手机号模型
	 * @param mobile      mobile
	 * @return array
	 */
	public function _mobile_ver($mobile,$user_id=''){

		$t = $this->user_table;
		$where = array('mobile'=>$mobile,'sid'=>$this->sid);//,'sid'=>$this->sid
		if(empty($user_id))
		{
			$query = $this->db->get_where($t,$where,1);
		}
		else
		{
			if($user_id)
			{

			}
			$where['user_id !='] = $user_id ;
			$query = $this->db->get_where($t,$where,1);
		}
		$row = $query->row_array();
		if($row)
		{
			return  array('status'=>'0','info'=>'手机号已经注册','msg'=>'mobile_repeat');
		}
		return  array('status'=>'1','info'=>TRUE,'msg'=>'mobile_pass');
	}


	/**
	 * 验证用户名模型
	 * @param mobile      username
	 * @return array
	 */
	public function _username_ver($user_name,$user_id='')
	{
		$t = $this->user_table;
		$where = array('username'=>$user_name,'sid'=>$this->sid);//,'sid'=>$sid
		if($user_id)
		{
			$where['user_id !='] = $user_id ;
		}
		$query = $this->db->get_where($t,$where,1);
		$row = $query->row_array();
		if($row)
		{
			return  array('status'=>'0','info'=>'用户名重复','msg'=>'username_repeat');
		}
		return  array('status'=>'1','info'=>TRUE,'msg'=>'username_pass');
	}

	/**
	 * 验证手机号存在模型
	 * @param mobile      mobile
	 * @return array
	 */
	public function _mobile_exist($mobile){

		$t = $this->user_table;
		$where = array('mobile'=>$mobile,'sid'=>$this->sid);//,'sid'=>$sid
		$query = $this->db->get_where($t,$where,1);
		$row = $query->row_array();
		if($row)
		{
			return  array('status'=>'1','info'=>TRUE,'msg'=>'mobile_exist');
		}
		return  array('status'=>'0','info'=>'手机号不存在','msg'=>'mobile_no_exist');
	}

	/**
	 * 验证邮箱存在模型
	 * @param Email     email
	 * @return array
	 */
	public function _email_exist($email){

		$t = $this->user_table;
		$where = array('email'=>$email,'sid'=>$this->sid);//,'sid'=>$sid
		$query = $this->db->get_where($t,$where,1);
		$row = $query->row_array();
		if($row)
		{
			return  array('status'=>'1','info'=>TRUE,'msg'=>'email_exist');
		}
		return  array('status'=>'0','info'=>'邮箱不存在','msg'=>'email_no_exist');
	}



	/**
	 * 验证积分规则操作
	 * @param action
	 * @return true/false
	 */
	public function _integral_action_ver($where=array())
	{
		$t = $this->integral_table;
		$this->db->where($where);
		$query = $this->db->get($t,1);
		$row = $query->row_array();
		if($row)
		{
			return  array('action_repeat','502','规则操作重复');
		}
		return  array('action_pass','200',true);
	}


	public function _identify_ver($identify)         // 验证码
	{
		$identify =strtolower(trim($identify));
		if(md5($identify) != $this->session->userdata('verifyCode'))
		{
			return  array('identify_error','502','验证码错误');
		}
		return  array('identify_pass','200',true);
	}

	/**
	 * 随机6位字符串
	 * @return char
	 */
	public function _rand_6_char()
	{
		$randStr = str_shuffle('1234567890');
		$rand = substr($randStr,0,6);
		return $rand ;
	}

	/**
	 * 随机4位字符串
	 * @return char
	 */
	public function _rand_4_char()
	{
		$randStr = str_shuffle('1234567890');
		$rand = substr($randStr,0,4);
		return $rand ;
	}



	/**
	 *
	 * 获取总数量
	 * @param array $where 查询条件
	 */
	public function get_member_count($where='')
	{
		$t = $this->db->dbprefix('user');
		$this->db->select('user_id');
		$this->db->where($where);
		$this->db->from($t);
		return $this->db->count_all_results() ;
	}

	/**
	 * 获取所有会员
	 * @param array $where 选择条件
	 */

	public function get_member_list($where='',$page_size=20,$now_page=1)
	{
		$t = $this->db->dbprefix('user');
		$this->db->select('user_id,username,mobile,email,reg_date,login_date,reg_ip,login_ip,status');
		$this->db->order_by('reg_date','desc');
		$this->db->where($where);
		$query = $this->db->get($t,$page_size,($now_page-1)*$page_size);
		return $query->result_array();
	}



	/**
	 *
	 * 获取规则
	 * @param array $where 查询条件
	 */
	public function get_integral_rule_count($where=array(),$extra='')
	{
		$t = $this->integral_table;
		$this->db->select('rid');
		if($extra)
		{
			$where['sid'] = $this->sid ;
		}

		$this->db->where($where);
		$this->db->from($t);
		return $this->db->count_all_results() ;
	}

	/**
	 * 获取所有积分规则
	 * @param array $where 选择条件
	 */

	public function get_integral_rule_list($where=array(),$page_size=20,$now_page=1,$extra='')
	{
		$t = $this->integral_table;
		$this->db->select('rid,rulename,action,integral');
		if($extra)
		{
			$where['sid'] = $this->sid ;
		}
		$this->db->where($where);
		$query = $this->db->get($t,$page_size,($now_page-1)*$page_size);
		return $query->result_array();
	}



	/**
	 * 验证手机号格式
	 * @param  mobile
	 * @return true/false
	 */

	public function _check_is_mobile($mobile)
	{
		if(preg_match("/1[34578]{1}\d{9}$/",$mobile))
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
	}

	/**
	 * 验证邮箱格式
	 * @param email
	 * @return true/false
	 */

	public function _check_is_email($email)
	{
		$pattern = "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i";
		if (preg_match($pattern,$email))
		{
			return TRUE ;
		}
		return FALSE ;
	}

	/**
	 * 验证用户名格式
	 * @param username
	 * @return true/false
	 */

	public function _check_is_username($username)
	{
		$pattern = "/^[a-z0-9_\x{4e00}-\x{9fa5}]{2,16}$/ui";
		if(preg_match($pattern,$username))
		{
			return TRUE ;
		}
		return FALSE ;
	}


	/**
	 * 发送邮件
	 * @param email
	 * @param subject
	 * @param content
	 * @return true/false
	 */

	public function _send_email($email,$subject,$content)
	{
        $this->load->library('email');
        $mail_user = $this->data['mail_user'] ;
        $mail_password = $this->data['mail_password'] ;
        if(empty($mail_user)||empty($mail_password)) return false ;
        $config['protocol'] = 'smtp';
        $config['charset'] = 'utf-8';
        $config['wordwrap'] = TRUE;
        $config['mailtype'] = 'html';
        $config['smtp_host'] = $this->data['mail_server'];
        $config['smtp_user'] = $mail_user ;
        $config['smtp_pass'] = $mail_password;
        $config['smtp_port'] = $this->data['mail_port'];
        $config['smtp_timeout'] = '50';
        $this->email->initialize($config);
        $this->email->from($this->data['mail_user'],$this->data['mail_fname']);
        $this->email->to($email);
        $this->email->subject($subject);
        $this->email->message($content);
        return $this->email->send();
	}

	/**
	 * 发送短信
	 * @param mobile
	 * @param content 数组
	 * @param templateid 阿里短信模板id
	 * @return 流水号 17位
	 */

	/*public function _send_sms($mobile,$templateid,$content){

		if(!$this->_check_is_mobile($mobile))
		{
			return False ;
		}


		$param = array(
            'appkey'=>'e79768a93fea6e5cf051d0fb2b766319',
            'phone'=> $mobile,
            'templateid'=> $templateid,
            'param'=>$content
        );

		$post = http_build_query($param);
        $res = _dfsockopen('http://api.id98.cn/api/sms', 0, $post);

		$string = (array)json_decode($res);
 		if(!$string['errcode'])
		{
            $this->_sms_log($this->sid,$mobile,$content,date('Y-m-d H:i:s'),$string[2]);
			return 1 ;
		}
		$this->_sms_log($this->sid,$mobile,$content,date('Y-m-d H:i:s'),$res);
	}*/


	public function _send_sms($mobile,$templateid,$content)
	{
		include_once APPPATH.'libraries/Tapi/TopSdk.php';
		$c = new TopClient;
		$c->appkey = '23390326';
		$c->secretKey = '67c4736c304b46ad68cd879188e3d845';
		$req = new AlibabaAliqinFcSmsNumSendRequest;
//		$req->setExtend("123456");
		$req->setSmsType("normal");
		$req->setSmsFreeSignName("微农家");
		if (!empty($content)) $req->setSmsParam(json_encode($content));
		$req->setRecNum($mobile);
		$req->setSmsTemplateCode($templateid);
		$resp = $c->execute($req);
		$resp = (array)$resp ;
		$result = (array)$resp['result'];

		if(!$result['err_code'])
		{
			$this->_sms_log($this->sid,$mobile,json_encode($content),date('Y-m-d H:i:s'),$result['err_code']);
			return true ;
		}

	}

	public function _send_more_sms($str,$content){

		if(empty($str)){
			return  false;
		}
		$nums = explode(",", $str);
		$mobile = '';
		foreach($nums as $val){
				
			if($this->_check_is_mobile($val))
			{
				$mobile.=','.$val;
			}
		}
		$mobile = trim($mobile,",");
		if(empty($mobile)){
			return  false;
		}
		$this->load->library('Curl');
        $sms_account = 'SDK-GGH-010-00155' ;
		$sms_pwd = 'fC582335' ;
		if(empty($sms_account)||empty($sms_pwd)) return false ;
		$param = array('sn'=>$sms_account,
            'pwd'=>strtoupper(md5(trim($sms_account.$sms_pwd))),
            'mobile'=> $mobile,
            'content'=>iconv( "UTF-8", "gb2312//IGNORE" ,$content),
            'ext'=>'',
            'rrid'=>'',
            'stime'=>''
            ) ;
		if($this->data['sms_extra_check'])
		{
			$param['ext'] =  $this->data['sms_extra'];
		}
            $post = http_build_query($param);
            $res = _dfsockopen('http://sdk2.zucp.net:8060/webservice.asmx/mt', 0, $post);
            // $res = $this->curl->simple_post('http://sdk2.zucp.net:8060/webservice.asmx/mt',$param,array(CURLOPT_BUFFERSIZE => 10));
        preg_match("/(org\/\")(.+)(<\/string)/",$res,$string);
        if(!strpos($string[2],'-'))
        {
            $this->_sms_log($this->sid,$str,$content,date('Y-m-d H:i:s'),$string[2]);
            return 1 ;
        }
		return $res;
	}

	/**
	 * 积分规则操作
	 * @param action
	 * @param user_id
	 * @return true/false
	 */

	public function _integral_operate($action,$user_id)
	{
		$t = $this->user_expand_table;
		$sid = $this->sid;
		$integral = $this->_integral_val(array('action'=>$action,'sid'=>$sid));
		$this->db->select('integral');
		$this->db->where(array('user_id'=>$user_id));
		$query = $this->db->get($t,1);
		$res = $query->row_array();
		if($res)
		{
			$pre = $res['integral'] ;
			$this->db->where(array('user_id'=>$user_id));
			$integral = $integral+$pre ;
			$data = array('integral'=>$integral) ;
			return $this->db->update($t,$data);
		}
		$array = array('user_id'=>$user_id,'integral'=>$integral);
		return $this->db->insert($t,$array);
	}

	/**
	 * 获取积分操作积分值
	 * @param action
	 * @return 积分值
	 */

	public function _integral_val($where=array())
	{
		$t = $this->integral_table;
		$this->db->select('integral');
		$this->db->where($where);
		$query = $this->db->get($t,1);
		$res = $query->row_array();
		if($res){
			return @$res['integral'] ;
		}
		return false ;
	}



    /**
     * 编辑会员/微店基础信息
     * @param $info_array
     * @return true/false
     */
    public function _edit_member($data,$where)
    {
        $t = $this->user_table;
        if(!empty($where))$this->db->where($where);
        return $this->db->update($t,$data);
        return false;
    }


    /**
     * 编辑会员/微店扩展信息
     * @param $info_array
     * @return true/false
     */
    public function _edit_member_expand($data,$where)
    {
        $t= $this->db->dbprefix('user_expand');
        if(!empty($where))$this->db->where($where);
        return $this->db->update($t,$data);
        return false;
    }

	/**
	 * 获取线路图表数据
	 * @param string $format 数据格式
	 * @param $where 查询条件
	 * @return mixed
	 */
	public function user_chart($format="'%Y-%m-%d'",$where=''){
		$sql = "select DATE_FORMAT(reg_date,$format) as date,count(*) as user_num from ".$this->user_table;
		$sql.= " WHERE sid=".$this->sid;
		$sql.= $where?" AND $where":'';
		$sql.= " group by DATE_FORMAT(reg_date,$format) ORDER BY date ASC";
		$query = $this->db->query($sql);
		$list =  $query->result_array();
		$data = array();
		if(!empty($list)){
			foreach($list as $key=>$val){
				$data[$val[date]] = intval($val['user_num']);
			}
		}
		return $data;
	}


    /**
     * 获取用户收藏
     * @param array $where 选择条件
     */

    public function get_favourite_list($where=array(),$page_size=20,$now_page=1)
    {

        $this->db->select("$this->user_favourite_table.*");
        $this->db->order_by("$this->user_favourite_table.crts",'desc');
        if(!empty($where))
        {
            switch($where['type'])
            {
                case '1':

                    $this->db->join($this->line_table, "$this->user_favourite_table.favourite_id = $this->line_table.line_id");
                    $condition = "$this->user_favourite_table.type = '1' and $this->user_favourite_table.user_id = {$where['user_id']} and $this->line_table.status=1";
                    break;
                case '2':
                    $this->db->join($this->scenic_table, "$this->user_favourite_table.favourite_id = $this->scenic_table.id");
                    $condition = "$this->user_favourite_table.type = '2' and $this->user_favourite_table.user_id = {$where['user_id']} ";
                    break;

                case '3':
                    $this->db->join($this->car_info_table, "$this->user_favourite_table.favourite_id = $this->car_info_table.id");
                    $condition = "$this->user_favourite_table.type = '3' and $this->user_favourite_table.user_id = {$where['user_id']} and $this->car_info_table.status=1";
                    break;
            }
            $condition .= " and $this->user_favourite_table.sid= {$GLOBALS['sid']}";
            $this->db->where($condition,NULL, FALSE);
            $query = $this->db->get($this->user_favourite_table,$page_size,($now_page-1)*$page_size);

        }


           return $query->result_array();
    }


    /**
     * 获取用户收藏
     * @param array $where 选择条件
     */

    public function get_favourite_row($where=array())
    {
        if(!empty($where))
        {
            $query = $this->db->get_where($this->user_favourite_table, $where,'1');
            return $query->row_array();
        }
    }

    /**
     * 添加收藏
     * @param $info_array
     * @return true/false
     */
    public function _add_favourite($array='')
    {
        if(!empty($array))
        {
            $this->db->insert($this->user_favourite_table,$array);
            return $this->db->insert_id();
        }
        return false;
    }

    /**
     *
     * 删除收藏
     * @param array $where 删除条件
     */

    public function _delete_favourite($where=array())
    {
        if(!empty($where)&&is_array($where))
        {
            return $this->db->delete($this->user_favourite_table,$where);

        }
        return false;
    }

    /**
     *
     * 短信日志
     */

    public function _sms_log($sid,$mobile,$content,$date,$return)
    {
        $this->load->helper('file');
        $data = "※\t 站点ID:".$sid."\t 手机号:".$mobile."\t 内容:".$content."\t时间:".$date."\t返回信息:".$return."\n";
        write_file(APPPATH.'logs/sms_log.log', $data,'ab+');
    }
}

